﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Vara.Utility
{
    public class VaraSqlEngineFacade : ISqlEngine
    {
        private string _conenctionStringName = string.Empty;
        public string ConnectionStringName
        {
            get
            {
                return _conenctionStringName;
            }
            set
            {
                _conenctionStringName = value;
            }
        }

        public VaraSqlEngineFacade()
        {

        }
        public VaraSqlEngineFacade(string connectionStringName)
        {
            this.ConnectionStringName = connectionStringName;
        }

        public System.Data.DataTable ExecuteProcedure(string name)
        {
            return this.ExecuteProcedure(name, null);
        }

        public System.Data.DataTable ExecuteProcedure(string name, params ISqlEngineVariable[] variables)
        {
            DataTable newTable = null;
            using (VaraSqlEngine Engine = this.getSqlEngineInstance())
            {
                
                newTable = variables == null ? 
                    Engine.ExecuteProcedure(name) :
                    Engine.ExecuteProcedure(name, this.convertParamList(variables));
            }
            return newTable;
        }

        public bool ProcedureContainsData(string name)
        {
            throw new NotImplementedException();
        }

        public bool ProcedureContainsData(string name, params ISqlEngineVariable[] variables)
        {
            bool containsData = false;
            using (VaraSqlEngine Engine = this.getSqlEngineInstance())
            {
                containsData = variables == null ?
                    Engine.ProcedureContainsData(name) :
                    Engine.ProcedureContainsData(name, this.convertParamList(variables));
            }
            return containsData;
        }

        public System.Data.DataRow ReturnProcedureRow(string name)
        {
            return this.ReturnProcedureRow(name, 0);
        }

        public System.Data.DataRow ReturnProcedureRow(string name, int row)
        {
            return this.ExecuteProcedure(name).Rows[row];
        }

        public System.Data.DataRow ReturnProcedureRow(string name, params ISqlEngineVariable[] variables)
        {
            return this.ReturnProcedureRow(name, 0, variables);
        }

        public System.Data.DataRow ReturnProcedureRow(string name, int row, params ISqlEngineVariable[] variables)
        {
            return this.ExecuteProcedure(name, variables).Rows[row];
        }

        private VaraSqlEngine.VaraSqlEngineVariable[] convertParamList(ISqlEngineVariable[] vars)
        {
            List<VaraSqlEngine.VaraSqlEngineVariable> returnValue = new List<VaraSqlEngine.VaraSqlEngineVariable>();
            foreach (VaraSqlEngineFacadeVariable v in vars)
            {                
                returnValue.Add(v.Type.HasValue ?
                   new VaraSqlEngine.VaraSqlEngineVariable(v.Name, v.Value, v.Type.Value) :                    
                    new VaraSqlEngine.VaraSqlEngineVariable(v.Name,v.Value.ToString()));
            }
            return returnValue.ToArray();
        }

        private VaraSqlEngine getSqlEngineInstance()
        {
            return string.IsNullOrEmpty(this.ConnectionStringName) ? 
                new VaraSqlEngine() : 
                new VaraSqlEngine(this.ConnectionStringName);
        }

        public void Dispose()
        {

        }

        public class VaraSqlEngineFacadeVariable : ISqlEngineVariable
        {
            #region ISqlEngineVaraiable Implementation
            private string _name = string.Empty;
            private object _value = null;
            private SqlDbType? _type =null;

            public string Name
            {
                get
                {
                    return _name;
                }
                set
                {
                    _name = value;
                }
            }

            public object Value
            {
                get
                {
                    return _value;
                }
                set
                {
                    _value = value;
                }
            }

            public System.Data.SqlDbType? Type
            {
                get
                {
                    return _type;
                }
                set
                {
                    _type = value;
                }
            }
            #endregion

            public VaraSqlEngineFacadeVariable()
            {
            }

            public VaraSqlEngineFacadeVariable(string name, string value)
            {
                this.Name = name;
                this.Value = value;
            }

            public VaraSqlEngineFacadeVariable(string name,object value)
            {
                this.Name = name;
                this.Value = value;
            }

            public VaraSqlEngineFacadeVariable(string name,object value,SqlDbType type)
            {
                this.Name = name;
                this.Value = value;
                this.Type = type;
            }
        }
    }
}
